﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace Royaumes_Libres.Database
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks>Patrick Chabot</remarks>
    public class cWorks
    {
        protected int pFkKingdom;
        protected int pDisciple;
        protected int pLumberjack;
        protected int pMiner;
        protected int pFarmer;
        protected int pWorker;

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// <remarks>Patrick Chabot</remarks>
        public override string ToString()
        {
            string str = "Disciple=" + this.pDisciple.ToString();
            str += "<br/>Lumberjack=" + this.pLumberjack.ToString();
            str += "<br/>Miner=" + this.pMiner.ToString();
            str += "<br/>Farmer=" + this.pFarmer.ToString();
            str += "<br/>Worker=" + this.pWorker.ToString();

            return str;
        }

        public int Farmer { get { return this.pFarmer; } }
        public int Miner { get { return this.pMiner; } }
        public int Worker { get { return this.pWorker; } }
        public int Disciple { get { return this.pDisciple; } }
        public int Lumberjack { get { return this.pLumberjack; } }

        /// <summary>
        /// 
        /// </summary>
        /// <remarks>Patrick Chabot</remarks>
        protected cWorks() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PkKingdom"></param>
        /// <returns></returns>
        /// <remarks>Patrick Chabot</remarks>
        public static cWorks GetWorks(int PkKingdom)
        {
            SqlCommand cmd = cSQL.GetDbJeuCommand();

            try
            {
                cmd.Connection.Open();
                cmd.CommandText = "cde.spGetWorks";
                cmd.Parameters.Add(cSQL.Param("@PkKingdom", PkKingdom, SqlDbType.Int));
                SqlDataReader dr = cmd.ExecuteReader();
                dr.Read();

                cWorks works = new cWorks();
                works.pFkKingdom = dr.GetInt32(0);
                works.pDisciple = dr.GetInt32(1);
                works.pLumberjack = dr.GetInt32(2);
                works.pMiner = dr.GetInt32(3);
                works.pFarmer = dr.GetInt32(4);
                works.pWorker = dr.GetInt32(5);

                cmd.Connection.Close();
                return works;
            }
            catch(Exception ex)
            {
                cmd.Connection.Close();
                throw new Exception("cWorks.GetWorks(int PkKingdom): " + ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PkKingdom"></param>
        /// <param name="Disciple"></param>
        /// <param name="Lumberjack"></param>
        /// <param name="Miner"></param>
        /// <param name="Farmer"></param>
        /// <param name="Worker"></param>
        /// <returns></returns>
        /// <remarks>Patrick Chabot</remarks>
        public static bool SetWorks(int PkKingdom, int Disciple, int Lumberjack, int Miner, int Farmer, int Worker)
        {
            SqlCommand cmd = cSQL.GetDbJeuCommand();

            try
            {
                cmd.Connection.Open();
                cmd.CommandText = "cde.spSetWorks";
                cmd.Parameters.Add(cSQL.Param("@PkKingdom", PkKingdom, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@Disciple", Disciple, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@Lumberjack", Lumberjack, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@Miner", Miner, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@Farmer", Farmer, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@Worker", Worker, SqlDbType.Int));

                int result = cmd.ExecuteNonQuery();
                cmd.Connection.Close();

                if (result == 1) { return true; }
                return false;
            }
            catch (Exception ex)
            {
                cmd.Connection.Close();
                throw new Exception("cWorks.SetWorks(int PkKingdom, int Disciple, int Lumberjack, int Miner, int Farmer, int Worker): " + ex.Message);
            }
        }
    }
}